unit UContasPagarDb;

interface

uses UPersistenciaSQL, UContasPagarMd, IBCustomDataSet, SysUtils;

type
   TContasPagarDb = class(TObject)

      private
         objPersistenciaSQL : TPersistenciaSQL;
         
      public
         function obterObjPersistenciaSQL : TPersistenciaSQL;
         function inserirContasPagar(pObjContasPagar : TContasPagarMd) : Boolean;
         function alterarContasPagar(pObjContasPagar : TContasPagarMd) : Boolean;
         function excluirContasPagar(pCodigo : Integer) : Boolean;
         function procurarContasPagar(pCodigo : Integer) : TContasPagarMd;
         function pagarContasPagar(pCodigo : Integer) : Boolean;         
         function obterContasPagar : TContasPagarMd;
         function obterListaContasPagar : TIBDataSet;
end;

implementation

function TContasPagarDb.obterObjPersistenciaSQL : TPersistenciaSQL;
begin
  if self.objPersistenciaSQL = nil then
    self.objPersistenciaSQL := TPersistenciaSQL.Create;
  result := self.objPersistenciaSQL;
end;

function TContasPagarDb.inserirContasPagar(pObjContasPagar : TContasPagarMd) : Boolean;
var
  sql : String;
  pSeparador : Char;
begin
  try
    pSeparador := DecimalSeparator;
    DecimalSeparator := '.';
    sql := 'insert into contaspagar (data_pagamento, valor, pago, observacao) values (' +
           QuotedStr(FormatDateTime('yyyy-mm-dd', pObjContasPagar.getDataPagamento)) + ', ' +
           FormatFloat('0.00', pObjContasPagar.getValor) + ', ''N'', ' +
           QuotedStr(pObjContasPagar.getObservacao) + ')';
    result := self.objPersistenciaSQL.executaUpdateQuery(sql);
    DecimalSeparator := pSeparador;
  except
  end;
end;

function TContasPagarDb.alterarContasPagar(pObjContasPagar : TContasPagarMd) : Boolean;
var
  sql : String;
  separador : Char;
begin
  try
    separador := DecimalSeparator;
    DecimalSeparator := '.';

    sql := 'update contaspagar set data_pagamento = ''' + FormatDateTime('yyyy-mm-dd', pObjContasPagar.getDataPagamento) + ''', ' +
           'valor = ' + FloatToStr(pObjContasPagar.getValor) + ', observacao = ' +
           QuotedStr(pObjContasPagar.getObservacao) + ' where id_contasPagar = ' +
           IntToStr(pObjContasPagar.getIdContasPagar);
    self.obterObjPersistenciaSQL.executaUpdateQuery(sql);

    DecimalSeparator := separador;    
  except
    on e : Exception do
    begin
      DecimalSeparator := separador;
      raise;
    end;    
  end;
end;

function TContasPagarDb.excluirContasPagar(pCodigo : Integer) : Boolean;
var
  sql : String;
begin
  try
    sql := 'delete from contaspagar where id_contasPagar = ' + IntToStr(pCodigo);
    self.obterObjPersistenciaSQL.executaUpdateQuery(sql);    
  except
    on e : Exception do
  end;
end;

function TContasPagarDb.procurarContasPagar(pCodigo : Integer) : TContasPagarMd;
var
  resultSet : TIBDataSet;
  pObjContasPagarMd : TContasPagarMd;
begin
  try
    pObjContasPagarMd := TContasPagarMd.Create;
    resultSet := self.objPersistenciaSQL.executaQuery('select * from contaspagar where id_contaspagar = ' + IntToStr(pCodigo));
    if (resultSet <> nil) then
    begin
      resultSet.First;
      pObjContasPagarMd.setIdContasPagar(resultSet.FieldValues['id_contaspagar']);
      pObjContasPagarMd.setDataPagamento(resultSet.FieldByName('data_pagamento').AsDateTime);
      pObjContasPagarMd.setValor(resultSet.FieldValues['valor']);

      result := pObjContasPagarMd;
    end;
    
  except
    on e : Exception do
    begin
      result := nil;
    end;
  end;
end;

function TContasPagarDb.pagarContasPagar(pCodigo : Integer) : Boolean;
var
  sql : String;
begin
  try
    sql := 'update contaspagar set pago = ''S'' where id_contaspagar = ' + IntToStr(pCodigo);
    self.obterObjPersistenciaSQL.executaUpdateQuery(sql);
  except
    on e : Exception do
  end;
end;

function TContasPagarDb.obterContasPagar : TContasPagarMd;
begin
end;

function TContasPagarDb.obterListaContasPagar : TIBDataSet;
begin
  try
    result := self.obterObjPersistenciaSQL.carregarBaseDeDados('contaspagar');
  except
    on e : Exception do
      result := nil;
  end;
end;

end.
 